﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Diagnostics; //---for Debug.WriteLine()---
using System.Device.Location;

namespace Demo_Bingmap
{
    public partial class MainPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
           
        }
        // Theo doi thay doi vi tri
        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
          switch (e.Status)
          {
            case GeoPositionStatus.Disabled:
            //Debug.WriteLine("disabled");
            txtStatus.Text="disabled";
            break;
            case GeoPositionStatus.Initializing:
            txtStatus.Text="initializing";
            break;
            case GeoPositionStatus.NoData:
            txtStatus.Text="nodata";
            break;
            case GeoPositionStatus.Ready:
            txtStatus.Text="ready";
            break;
            default:
            txtStatus.Text = "";
            break;

           }
        }
        
        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Debug.WriteLine("({0},{1})", 
            e.Position.Location.Latitude, e.Position.Location.Longitude);
 
            map1.Center = new GeoCoordinate(
            e.Position.Location.Latitude, e.Position.Location.Longitude);
            //Danh dau vi tri
            Pushpin pin = new Pushpin();

            //---set the location for the pushpin---
            pin.Location = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
            //---add the pushpin to the map---
            map1.Children.Add(pin);
        }
        private void btnRoad_Click(object sender, RoutedEventArgs e)
        {
            map1.Mode = new RoadMode();
           
        }

        private void btnAerial_Click(object sender, RoutedEventArgs e)
        {
            map1.Mode = new AerialMode();

        }

        private void btnZoomOut_Click(object sender, RoutedEventArgs e)
        {
            double zoom;
            zoom = map1.ZoomLevel;
            map1.ZoomLevel=++zoom;
        }

        private void btnZoomIn_Click(object sender, RoutedEventArgs e)
        {
            double zoom;
            zoom = map1.ZoomLevel;
            map1.ZoomLevel = --zoom;
        }
        // Locate me
        private void btnLocateMe_Click(object sender, RoutedEventArgs e)
        {
            //if (watcher == null)
            //{
                //---get the highest accuracy---
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
                {
                    //---the minimum distance (in meters) to travel before the next 
                    // location update---
                    MovementThreshold = 10
                };

                //---event to fire when a new position is obtained---
                watcher.PositionChanged += new
                EventHandler<GeoPositionChangedEventArgs
                <GeoCoordinate>>(watcher_PositionChanged);

                //---event to fire when there is a status change in the location 
                // service API---
                watcher.StatusChanged += new
                EventHandler<GeoPositionStatusChangedEventArgs>
                (watcher_StatusChanged);
                watcher.Start();
                
            //}
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            map1.Center = new GeoCoordinate(21.14130, 105.825898); // Hanoi area
            map1.ZoomLevel = 10;
        }
        // 
        private void map1_Loaded(object sender, RoutedEventArgs e)
        {
            map1.Center = new GeoCoordinate(21.14130, 105.825898); // Hanoi area
            map1.ZoomLevel = 10;
        }

    }
   
   
}